package com.walker.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpannerUtil {
	public static String DValue(String dateUnit,int DV) throws ParseException {
		dateUnit = dateUnit.toLowerCase();
		String dataV = null;
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		switch (dateUnit) {
		case "year":
			date.add(Calendar.YEAR, -DV);
			dataV = new SimpleDateFormat("yyyy").format(date.getTime());
			break;
		case "month":
			date.add(Calendar.MONTH, -DV);
			dataV = new SimpleDateFormat("yyyy-MM").format(date.getTime());
			break;
		case "week":
			date.add(Calendar.WEDNESDAY, -DV);
			dataV = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());
			break;
		case "day":
			date.add(Calendar.DATE, -DV);
			dataV = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());
			break;
		case "hour":
			date.add(Calendar.HOUR, -DV);
			dataV = new SimpleDateFormat("hh").format(date.getTime()); //小时分区,作为子分区,使用两位
			break;
		default:
			break;
		} 
		return dataV;
	}
	
	public static String getSysdate() {
		java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		return sdf.format(now);
	}
	
	public static int getCharacterPosition(String separator,String string,int Idx){
//	    Matcher slashMatcher = Pattern.compile("_").matcher(string);
		Matcher slashMatcher = Pattern.compile(separator).matcher(string);
	    int mIdx = 0;
	    int index = 0;
	    while(slashMatcher.find()) {
	       mIdx++;
	       if(mIdx == Idx){
	          break;
	       }
	    }
	    try {
	    	 index = slashMatcher.start();
		} catch (Exception e) {
			index = 0;
		}
	    return index;
	 }
}
